package com.zlj.accountant.core;

import java.util.ArrayList;
import java.util.List;

import org.omg.CORBA.TRANSACTION_MODE;

import com.zlj.accountant.db.dao.DBHelper;
import com.zlj.accountant.db.entity.TAbstractTable;

public abstract class AbstractService{
	
	public abstract class SimplerHandler<TTask extends SimpleTask> extends TaskHandler<TTask, None> {

		public SimplerHandler(){
	        super(None.NONE);
        }		

	}
	
	protected abstract class TaskHandler<TTask extends Task<TMethod>,TMethod extends Enum<TMethod>> extends AbstractHandler{
		
		public abstract void handle(HandlerManager handler,TTask task) throws Exception;
		
		private TMethod method;
		
		public TMethod getMethod(){
			return method;
		}
				
		public TaskHandler(TMethod method){
			this.method = method;
		}
		
	}
	
	protected abstract class OneKeyResultProvider<TResult,OneKey> extends AbstractHandler{
		public abstract TResult provide(HandlerManager handler,OneKey key) throws Exception;
	}
	
	protected abstract class ResultProvider<TResult> extends OneKeyResultProvider<TResult, Void>{
		@Override
		public TResult provide(HandlerManager handler,Void key)throws Exception{
		    return provide(handler);
		}

		public abstract TResult provide(HandlerManager handler)throws Exception;
	}
	
	protected abstract class OneKeyResultListProvider<TResult,OneKey> extends AbstractHandler{
		
		public List<TResult> provide(HandlerManager handler,OneKey key) throws Exception{
			List<TResult> result = new ArrayList<TResult>();
			provide(handler,result, key);
			return result;
		}
		
		public abstract void provide(HandlerManager handler,List<TResult> list,OneKey key)throws Exception;
	}
	
	protected abstract class ResultListProvider<TResult> extends OneKeyResultListProvider<TResult, Void>{
		
		public List<TResult> provide(HandlerManager handler)throws Exception{
			List<TResult> result = new ArrayList<TResult>();
			provide(handler,result,null);
			return result;
		}

		@Override
		public void provide(HandlerManager handler, List<TResult> list, Void key)
		        throws Exception
		{
		    provide(handler, list);
		}
		
		public abstract void provide(HandlerManager handler,List<TResult> list)throws Exception;
	}

	protected DBHelper dbHelper;

	public AbstractService(DBHelper dbHelper){
		this.dbHelper = dbHelper;
	}

	public DBHelper getDbHelper(){
		return dbHelper;
	}

	protected void setSystemInfo(TAbstractTable t){
		t.setCreateTime(System.currentTimeMillis());
		t.setCreator("13983212302");
	}

}
